package Utils;

import org.apache.commons.codec.binary.Hex;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.SecureRandom;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/7/5 11:01
 * @since JDK 1.8
 */
public class PassWordUtils {



    private static final Charset CHARSET = Charset.forName("UTF-8");

    private static SecureRandom random = new SecureRandom();

    public static byte[] utf8encode(CharSequence string) {
        try {
            ByteBuffer bytes = CHARSET.newEncoder().encode(CharBuffer.wrap(string));
            byte[] bytesCopy = new byte[bytes.limit()];
            System.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit());

            return bytesCopy;
        } catch (CharacterCodingException e) {
            throw new IllegalArgumentException("Encoding failed", e);
        }
    }

    public static String digest(String plainCredentials, byte[] salt) {
        byte[] hashPassword = digest(utf8encode(plainCredentials), salt);
        return encodeHex1(hashPassword);
    }


    protected static byte[] digest(byte[] input, byte[] salt) {
        return sha1(input, salt, 1024);
    }

    public static byte[] sha1(byte[] input, byte[] salt, int iterations) {
        return digest(input, "SHA-1", salt, iterations);
    }


    private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
        try {
            MessageDigest digest = MessageDigest.getInstance(algorithm);

            if (salt != null) {
                digest.update(salt);
            }

            byte[] result = digest.digest(input);

            for (int i = 1; i < iterations; i++) {
                digest.reset();
                result = digest.digest(result);
            }
            return result;
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        }
    }


    public static String encodeHex1(byte[] input) {
        return Hex.encodeHexString(input);
    }


    public static String entryptPassword(byte[] saltBytes, String rawPass) {
        String encPass = digest(rawPass, saltBytes);
        return encPass;
    }


    public static String getSaltStr(byte[] saltBytes) {
        String salt = encodeHex(saltBytes);
        return salt;
    }

    public static String encodeHex(byte[] input) {
        return Hex.encodeHexString(input);
    }


    public static byte[] generateSalt(int numBytes) {
        isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes);

        byte[] bytes = new byte[numBytes];
        random.nextBytes(bytes);
        return bytes;
    }

    public static void isTrue(final boolean expression, final String message, final long value) {
        if (!expression) {
            throw new IllegalArgumentException(String.format(message, Long.valueOf(value)));
        }
    }

    public static void main(String[] args) {
        byte[] salt=generateSalt(8);
        String salt1=getSaltStr(salt);
        String password=entryptPassword(salt,"12345678");
        System.out.println("密钥: "+salt1);
        System.out.println("密码: "+password);
    }


}
